package com.collectplus.express.upgrade;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.model.CheckUpdateModel;

import droid.frame.activity.ActivityMgr;
import droid.frame.utils.lang.Str;

/**
 * 版本升级
 * 
 * @author coffee<br>
 *         2016-11-28下午5:47:07
 */
public class UpgradeActivity extends BaseActivity2 implements View.OnClickListener {

	private CheckUpdateModel item;

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		item = (CheckUpdateModel) getIntent().getSerializableExtra("item");
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.upgrade);
		super.findViewById();
		// 升级描述
		TextView content = (TextView) findViewById(R.id.upgrade_content);
		if (item != null && !Str.isEmpty(item.getDescription())) {
			content.setText(Html.fromHtml(item.getDescription()));
		}
		//
		findViewById(R.id.upgrade_close).setOnClickListener(this);
		findViewById(R.id.upgrade_sure).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.upgrade_close:
			// 强制更新
			if (item.getIsNeedUpdate() == 2) {
				ActivityMgr.finishAll();
			} else {
				finish();
			}
			break;
		case R.id.upgrade_sure:
			UpgradeBean upgradeBean = cast(item);
			UpgradeLogic logic = new UpgradeLogic();
			logic.processUpdate(getContext(), upgradeBean, null);
			break;
		default:
			break;
		}
	}

	private UpgradeBean cast(CheckUpdateModel item) {
		UpgradeBean upgradeBean = new UpgradeBean();
		upgradeBean.setDownloadUrl(item.getDownloadUrl());
		upgradeBean.setContent(item.getDescription());
		upgradeBean.setEnforce(item.getIsNeedUpdate() == 2);
		return upgradeBean;
	}
}
